<?php
App::uses('AppController', 'Controller');
/**
 * TicketAdvisories Controller
 *
 * @property TicketAdvisory $TicketAdvisory
 * @property PaginatorComponent $Paginator
 */
class TicketAdvisoriesController extends AppController {

/**
 * Components
 *
 * @var array
 */
	public $components = array('Paginator','RequestHandler');

	public function beforeFilter(){
		$this->Auth->allow('add','index');
		parent::beforeFilter();
	}
/**
 * index method
 *
 * @return void
 */
	public function index() {
		
		$conditions = array('TicketAdvisory.ticket_show'=> 1);
		
		$ticket_id =  isset($this->request->named['ticket'])?$this->request->named['ticket']:false;
		if($ticket_id){
			$conditions['TicketAdvisory.ticket_id'] = $ticket_id;
		}
		$this->TicketAdvisory->recursive = 0;
		$this->Paginator->settings = array(
				'conditions'=>$conditions,
				'order'=>'TicketAdvisory.created DESC',
				'limit'=>10
		);
		$this->set('ticketAdvisories', $this->Paginator->paginate('TicketAdvisory'));
		
		if($ticket_id){
			$this->loadModel('Ticket');
			$ticket = $this->Ticket->findById($ticket_id);
			$seo_title = $ticket['SysSeo']['title'].' 票品咨询';
			$this->set('ticket',$ticket);
		}else{
			$seo_title = '票品咨询';
			$this->set('ticket',false);
		}
		
		// 分类-------------------------------------------------------------
		$SITEID = Configure::read('SITEID');
		$this->loadModel('ArticleCate');
		$siteArticleCate = $this->ArticleCate->getSiteEnable($SITEID);
		$this->set('siteArticleCate',$siteArticleCate);
		
		// 热门活动-------------------------------------------------------------
		$this->loadModel('Article');
		$hotArticles = $this->Article->find('all',array(
				'conditions'=>array(
						'Article.article_cate_id'=>2
				),
				'limit'=>7,
				'order'=>'Article.publish DESC'
		));
		$this->set('hotArticles',$hotArticles);
		
		$this->set('seo_title',$seo_title);
		$this->set('seo_keywords',false);
		$this->set('seo_description',false);
		$this->set('seo_title_ext','【秦文票务演出订票官网】');
	}
	
	/**
	 * index method
	 *
	 * @return void
	 */
	public function my() {
		$this->TicketAdvisory->recursive = 0;
		$this->Paginator->settings = array(
				'conditions'=>array(
						'TicketAdvisory.user_id'=> $this->Auth->user('id'),
				),
				'order'=>'TicketAdvisory.created DESC',
				'limit'=>10
		);
		$this->set('ticketAdvisories', $this->Paginator->paginate('TicketAdvisory'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->TicketAdvisory->exists($id)) {
			throw new NotFoundException(__('Invalid ticket advisory'));
		}
		$options = array('conditions' => array('TicketAdvisory.' . $this->TicketAdvisory->primaryKey => $id));
		$this->set('ticketAdvisory', $this->TicketAdvisory->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$feedback = $this->Session->read('feedback');
			if($feedback == md5(strtolower($this->request->data['TicketAdvisory']['authcode']))){
				$this->TicketAdvisory->create();
				$this->loadModel('Lbs');
				$ip = $this->RequestHandler->getClientIP();
				$this->request->data['TicketAdvisory']['question_ip'] = $ip;
				$this->request->data['TicketAdvisory']['question_local'] =$this->Lbs->qqip($ip);
				
				if ($this->TicketAdvisory->save($this->request->data)) {
					$this->set('state',true);
					$this->set('msg','保存成功');
				} else {
					$this->set('state',false);
					$this->set('msg','提交失败，请重试');
				}
			}else{
				$this->set('state',false);
				$this->set('msg','验证码不正确');
			}
		}
	}


/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->TicketAdvisory->id = $id;
		if (!$this->TicketAdvisory->exists()) {
			throw new NotFoundException(__('Invalid ticket advisory'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->TicketAdvisory->delete()) {
			$this->Session->setFlash(__('The ticket advisory has been deleted.'));
		} else {
			$this->Session->setFlash(__('The ticket advisory could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index($ticket_id = false) {
		$this->TicketAdvisory->recursive = 0;
		$action = array('admin' => true,'controller' => 'TicketAdvisories','action'=>'jsonPage',$ticket_id);
		$param = $this->TicketAdvisory->pageParam($action);
		$this->set('paginationScript',$this->TicketAdvisory->showGridPagination($param));
		$this->set('ticket_id',$ticket_id);
	}
	
	/**
	 * function
	 * @author mingxing.zhao@qq.com
	 * @date 2013-12-21 下午5:05:53
	 * @param variable
	 * @return
	 */
	function admin_jsonPage($type = false){
		$this->TicketAdvisory->recursive = 0;
		$this->autoRender = false;
		$searchtype = $this->request->data('_search');
		$conditions = array();
		if($searchtype=='true'){
			$conditions = $this->search_condition($this->request->data);
		}
		if($type){
			$conditions['TicketAdvisory.ticket_id']= $type;
		}
		$conditions['Ticket.sys_website_id']= $this->Session->read('SITEID');
	
		$config = array(
				'_search'=>$searchtype,
				'page'=>$this->request->data('page'),
				'rows'=>$this->request->data('rows'),
				'sidx'=>$this->request->data('sidx'),
				'sord'=>$this->request->data('sord')
		);
		$res = $this->TicketAdvisory->pagination($config,array(),$conditions);
		return $this->TicketAdvisory->jsonData($res);
	}
	/**
	 * 查询条件
	 * @author zmx@gootop.net
	 * @Date 2011-2-23
	 * @return
	 */
	private function search_condition($param){
		$this->User->recursive = 0;
		$search['User'] = $param['User'];
		$c = $this->postConditions($search);
		$return = array();
		if($c['User.username'] != ''){
			$return['or'] = array(
					array('User.username like' =>'%'.$c['User.username'].'%'),
			);
		}
		if($c['User.email'] != ''){
			$return['User.email']= $c['User.email'];
		}
		if($c['User.mobile'] != ''){
			$return['User.mobile']= $c['User.mobile'];
		}
		if($c['User.user_state_id'] > 0){
			$return['User.user_state_id']= $c['User.user_state_id'];
		}
		if($c['User.from'] !='' && $c['User.to'] !=''){
			$return['AND']['User.created >=']= $c['User.from'];
			$return['AND']['User.created <=']= $c['User.to'].' 23:59:59';
		}else if($c['User.from'] !=''){
			$return['User.created >=']= $c['User.from'];
		}else if($c['User.to'] !=''){
			$return['User.created <=']= $c['User.to'].' 23:59:59';
		}
		return $return;
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->TicketAdvisory->exists($id)) {
			throw new NotFoundException(__('Invalid ticket advisory'));
		}
		$options = array('conditions' => array('TicketAdvisory.' . $this->TicketAdvisory->primaryKey => $id));
		$this->set('ticketAdvisory', $this->TicketAdvisory->find('first', $options));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add($ticket_id = false) {
		if (!$this->TicketAdvisory->Ticket->exists($ticket_id)) {
			throw new NotFoundException(__('Invalid ticket'));
		}
		if ($this->request->is('post')) {
			$this->TicketAdvisory->create();
			
			if($this->request->data['TicketAdvisory']['question_local'] == ''){
				$this->loadModel('Lbs');
				$ip = $this->request->data['TicketAdvisory']['question_ip'];
				$this->request->data['TicketAdvisory']['question_local'] =$this->Lbs->qqip($ip);
			}
			if ($this->TicketAdvisory->save($this->request->data)) {
				$this->Session->setFlash(__('咨询添加成功'),'tip/tip_float',array('t'=>'success'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('资讯添加失败，请重试'),'tip/tip_float',array('t'=>'error'));
			}
		}
		$this->TicketAdvisory->Ticket->recursive = -1;
		$ticket = $this->TicketAdvisory->Ticket->findById($ticket_id);
		$this->set('ticket',$ticket);
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->TicketAdvisory->exists($id)) {
			throw new NotFoundException(__('Invalid ticket advisory'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if($this->request->data['TicketAdvisory']['question_local'] == ''){
				$this->loadModel('Lbs');
				$ip = $this->request->data['TicketAdvisory']['question_ip'];
				$this->request->data['TicketAdvisory']['question_local'] =$this->Lbs->qqip($ip);
			}
			if ($this->TicketAdvisory->save($this->request->data)) {
				$this->Session->setFlash(__('咨询添加成功'),'tip/tip_float',array('t'=>'success'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('资讯添加失败，请重试'),'tip/tip_float',array('t'=>'error'));
			}
		} else {
			$options = array('conditions' => array('TicketAdvisory.' . $this->TicketAdvisory->primaryKey => $id));
			$this->request->data = $this->TicketAdvisory->find('first', $options);
		}
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->TicketAdvisory->id = $id;
		if (!$this->TicketAdvisory->exists()) {
			throw new NotFoundException(__('Invalid ticket advisory'));
		}
		//$this->request->onlyAllow('post', 'delete');
		if ($this->TicketAdvisory->delete()) {
			$this->Session->setFlash(__('The ticket advisory has been deleted.'),'tip/tip_float',array('t'=>'success'));
		} else {
			$this->Session->setFlash(__('The ticket advisory could not be deleted. Please, try again.'),'tip/tip_float');
		}
		return $this->redirect(array('action' => 'index'));
	}}
